package com.yitong.equipment.ui.home

import android.content.Intent
import android.graphics.Color
import android.os.Build
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.LinearLayout
import androidx.annotation.RequiresApi
import androidx.core.widget.NestedScrollView
import com.gyf.immersionbar.ImmersionBar
import com.scwang.smart.refresh.layout.api.RefreshHeader
import com.scwang.smart.refresh.layout.api.RefreshLayout
import com.yitong.equipment.R
import com.yitong.equipment.interf.AppOnMultiListener
import com.yitong.equipment.base.BaseFragment
import com.yitong.equipment.ui.viewmodel.HomeViewModel
import com.youth.banner.indicator.CircleIndicator
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import com.aliyun.player.source.UrlSource
import com.blankj.utilcode.util.LogUtils
import com.yitong.equipment.ext.init
import com.yitong.equipment.ui.adapter.*
import com.aliyun.player.widget.AliyunRenderView
import com.blankj.utilcode.util.SizeUtils
import com.blankj.utilcode.util.ToastUtils
import com.bumptech.glide.Glide
import com.yitong.equipment.databinding.FragmentHomeBinding
import com.yitong.equipment.ui.qrCode.QRScanActivity
import com.yitong.equipment.utils.CommonUtil
import com.youth.banner.adapter.BannerImageAdapter
import com.youth.banner.holder.BannerImageHolder
import com.youth.banner.listener.OnPageChangeListener


class HomeFragment : BaseFragment<HomeViewModel, FragmentHomeBinding>() {

    var imageUrls = listOf(
        "https://img.zcool.cn/community/01b72057a7e0790000018c1bf4fce0.png",
        "https://img.zcool.cn/community/016a2256fb63006ac7257948f83349.jpg",
        "https://img.zcool.cn/community/01233056fb62fe32f875a9447400e1.jpg",
        "https://img.zcool.cn/community/01700557a7f42f0000018c1bd6eb23.jpg"
    )
    //banner 坐标
    private val ivPointList:MutableList<View> = arrayListOf()

    private val homeMenuAdapter: HomeMenuAdapter by lazy { HomeMenuAdapter(arrayListOf()) }
    private val homeNewCourseAdapter: HomeNewCourseAdapter by lazy { HomeNewCourseAdapter(arrayListOf()) }
    private val homeTeacherAdapter: HomeTeacherAdapter by lazy { HomeTeacherAdapter(arrayListOf()) }
    private val homeHotCourseAdapter: HomeHotCourseAdapter by lazy { HomeHotCourseAdapter(arrayListOf()) }


    private var bannerHeight = 0


    override fun setContent(): Int {
        return R.layout.fragment_home
    }


    @RequiresApi(Build.VERSION_CODES.M)
    override fun initView() {
        ImmersionBar.setTitleBar(this, bindingView.homeTopLayout.toolbar)
        val bannerParams: ViewGroup.LayoutParams = bindingView.banner.layoutParams
        val titleBarParams: ViewGroup.LayoutParams = bindingView.homeTopLayout.toolbar.layoutParams
        bannerHeight = bannerParams.height - titleBarParams.height - ImmersionBar.getStatusBarHeight(this)
        bindingView.nestedScrollView.run {
            setOnScrollChangeListener(object : NestedScrollView.OnScrollChangeListener {
                var alpha = 0
                var scale = 0f
                override fun onScrollChange(v: NestedScrollView?, scrollX: Int, scrollY: Int, oldScrollX: Int, oldScrollY: Int) {
                    if (scrollY <= bannerHeight) {
                        scale = scrollY.toFloat() / bannerHeight
                        alpha = (255 * scale).toInt()
                        bindingView.homeTopLayout.toolbar.setBackgroundColor(setColor(alpha))
                        bindingView.homeTopLayout.LayoutExpand.visibility = View.VISIBLE
                        bindingView.homeTopLayout.LayoutContract.visibility = View.GONE
                        ImmersionBar.with(this@HomeFragment).statusBarDarkFont(false).init()
                    } else {
                        if (alpha < 255) {
                            alpha = 255
                            bindingView.homeTopLayout.toolbar.setBackgroundColor(setColor(alpha))
                            bindingView.homeTopLayout.LayoutExpand.visibility = View.GONE
                            bindingView.homeTopLayout.LayoutContract.visibility = View.VISIBLE
                            ImmersionBar.with(this@HomeFragment).statusBarDarkFont(true).init()
                        }
                    }
                }
            })
        }

        bindingView.homeTopLayout.toolbar.run {
            setBackgroundColor(setColor(0))
        }
        bindingView.refreshLayout.run {
            setEnableLoadMore(false)
            setOnMultiListener(object : AppOnMultiListener {
                override fun onRefresh(refreshLayout: RefreshLayout) {
                    bindingView.refreshLayout.finishRefresh()
                }

                override fun onHeaderMoving(header: RefreshHeader?, isDragging: Boolean, percent: Float, offset: Int, headerHeight: Int, maxDragHeight: Int) {
                    if (percent == 0f) {
                        bindingView.homeTopLayout.toolbar.visibility = View.VISIBLE
                    } else {
                        bindingView.homeTopLayout.toolbar.visibility = View.GONE
                    }
                }
            })
        }

        bindingView.recycleView.init(GridLayoutManager(activity, 4), homeMenuAdapter, false)

//        bindingView.homeTopLayout.llScan.setOnClickListener {
//            startActivity(Intent(activity, QRScanActivity::class.java))
//        }

        homeMenuAdapter.apply {
            setOnItemClickListener { adapter, view, position ->
                startActivity(Intent(activity, AllClassifyActivity::class.java))
            }
        }

        bindingView.recycleViewNewCourse.init(LinearLayoutManager(activity), homeNewCourseAdapter, false)
        bindingView.recycleViewHotCourse.init(GridLayoutManager(activity, 2), homeHotCourseAdapter, false)

        var linearLayoutManager = LinearLayoutManager(activity)
        linearLayoutManager.orientation = LinearLayoutManager.HORIZONTAL
        bindingView.recycleViewTeacher.init(linearLayoutManager, homeTeacherAdapter, true)

        var mutableListOf = mutableListOf<String>()
        for (index in 0..7) {
            mutableListOf.add("")
        }
        homeMenuAdapter.setList(CommonUtil.getHomeTopList())
        homeNewCourseAdapter.setList(mutableListOf)
        homeHotCourseAdapter.setList(mutableListOf)
        homeTeacherAdapter.setList(mutableListOf)

        homeNewCourseAdapter.setOnItemClickListener { adapter, view, position ->
            startActivity(Intent(activity, CourseDetailActivity::class.java))
        }


        bindingView.banner.apply {
            addBannerLifecycleObserver(activity)
            setIndicator(CircleIndicator(activity))
            setAdapter(object : BannerImageAdapter<String>(imageUrls) {
                override fun onBindView(holder: BannerImageHolder, data: String, position: Int, size: Int) {
                    Glide.with(activity!!)
                        .load(data)
                        .into(holder.imageView)
                }
            })
           adapter.setOnBannerListener { data, position ->
                //这里是点击事件
                ToastUtils.showShort(position)
            }

            addOnPageChangeListener(object : OnPageChangeListener{
                override fun onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int) {

                }

                override fun onPageSelected(position: Int) {
                    var length = imageUrls.size
                    for (index in 0 until length) {
                        ivPointList[position].setBackgroundResource(R.drawable.ashape_ffffff_2_corner);
                        if (position != index) {
                            ivPointList[index].setBackgroundResource(R.drawable.ashape_242526_2_corner);
                        }
                    }
                }

                override fun onPageScrollStateChanged(state: Int) {

                }

            })

            val size: Int = imageUrls.size
            for (index in 0 until size) {
                var ivPoint = ImageView(activity)
                var layoutParams = LinearLayout.LayoutParams(SizeUtils.dp2px(12f), SizeUtils.dp2px(4f))
                layoutParams.setMargins(0,0,10,0)
                ivPoint.layoutParams = layoutParams
                ivPointList.add(ivPoint)
                if (index == 0){
                    ivPoint.setBackgroundResource(R.drawable.ashape_ffffff_2_corner);
                }else{
                    ivPoint.setBackgroundResource(R.drawable.ashape_242526_2_corner);
                }
                bindingView.llPoint.addView(ivPointList[index])
            }
        }

    }

    override fun initData() {
        createObserver()
    }

    private fun createObserver() {

    }

    override fun firstLoad() {
        viewModel.getData()
    }

    override fun notFirstLoad() {
        LogUtils.dTag("notFirstLoad", "notFirstLoad")
    }

    override fun onDestroy() {
        super.onDestroy()
        viewModel.onCleared()
    }

    fun setColor(alpha: Int): Int {
        return Color.argb(alpha, 255, 255, 255)
    }
}


